//
//  PopSheetView.m
//  UILbrary
//
//  Created by Apple on 2023/3/23.
//  Copyright © 2023 mac. All rights reserved.
//

#import "PopSheetView.h"


@interface CustomTableViewCell : UITableViewCell

@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIView *lineView;

@end

@implementation CustomTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self configSubviews];
        [self test];
    }
    return self;
}

- (void)test{
//    self.label.backgroundColor = [UIColor orangeColor];
//    self.lineView.backgroundColor = [UIColor redColor];
}

- (void)configSubviews{
     [self.contentView addSubview:self.label];
    [self.contentView addSubview:self.lineView];

    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.contentView);
        make.bottom.equalTo(self.lineView.mas_top);
        make.height.mas_equalTo(50);
    }];

    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.contentView);
        make.height.mas_equalTo(1/[UIScreen mainScreen].scale);
    }];
}

- (UILabel *)label {
    if (!_label) {
        _label = [[UILabel alloc] init];
        _label.font = [UIFont systemFontOfSize:16];
        _label.textColor = [UIColor blackColor];
        _label.textAlignment = NSTextAlignmentCenter;
       
    }
    return _label;
}

- (UIView *)lineView {
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor lightGrayColor];
       
    }
    return _lineView;
}

@end


@interface PopSheetView()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, copy) NSString *title;
@property(nonatomic, strong) NSArray <NSString *>* list;
@property(nonatomic, copy) void(^selectBlock)(NSInteger idx, NSString *name);
@property(nonatomic, copy) void(^canselBlock)(void);


@property (nonatomic, strong) UIView *maskView;
@property (nonatomic, strong) UIView *containView;

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *canselButton;
@property (nonatomic, strong) UIButton *closeButton;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *spaceView;

@end


@implementation PopSheetView

+ (void)showAlertSheetViewWithTitle:(NSString *)title
                        contentList:(NSArray <NSString *>*)list
                        selectBlock:(void(^)(NSInteger idx, NSString *name))selectBlock
                        canselBlock:(void(^)(void))canselBlock
{
    
    NSLog(@"show");
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    PopSheetView *view = [PopSheetView new];
    [window addSubview:view];
    
    view.title = title;
    view.list = list;
    view.selectBlock = selectBlock;
    view.canselBlock = canselBlock;
    
    [view setUp];
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(window);
    }];
    
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self test];
        [self configSubviews];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(self.tableView.contentSize.height);
    }];
    
}

- (void)test{
    //    self.maskView.backgroundColor = [UIColor redColor];
//    self.containView.backgroundColor = [UIColor redColor];
//    self.canselButton.backgroundColor = [UIColor orangeColor];
    self.closeButton.backgroundColor = [UIColor purpleColor];
}

- (void)setUp
{
    self.titleLabel.text = self.title;
    [self.tableView reloadData];
}

- (void)configSubviews
{
    [self addSubview:self.maskView];
    [self addSubview:self.containView];
    [self.containView addSubview:self.titleLabel];
    [self.containView addSubview:self.canselButton];
    [self.containView addSubview:self.closeButton];
    [self.containView addSubview:self.tableView];
    [self.containView addSubview:self.spaceView];
    
    [self.maskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self).offset(15);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.containView);
        make.top.mas_equalTo(self.containView);
        make.height.mas_equalTo(40);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.mas_equalTo(self.containView);
        make.top.mas_equalTo(self.titleLabel.mas_bottom);
        make.height.mas_equalTo(1000);
    }];
    
    [self.spaceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.containView);
        make.height.mas_equalTo(10);
        make.top.mas_equalTo(self.tableView.mas_bottom);
    }];
    
    [self.canselButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.spaceView.mas_bottom).offset(0);
        make.left.right.mas_equalTo(self.containView);
        make.height.mas_equalTo(50);
        make.bottom.mas_equalTo(self.containView.mas_safeAreaLayoutGuideBottom);
    }];
    
   
    
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.containView).offset(0);;
        make.top.mas_equalTo(self.containView).offset(0);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40);
    }];
    
}

- (void)dismiss
{
    NSLog(@"dismiss");
    [self removeFromSuperview];
}

//MARK: - UITableViewDelegate UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell" forIndexPath:indexPath];
    cell.label.text = self.list[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.selectBlock) {
        self.selectBlock(indexPath.row, self.list[indexPath.row]);
    }
    [self dismiss];
}

//MARK: - Action

- (void)tapAction
{
    NSLog(@"tapAction");
    [self dismiss];
}

- (void)canselAction
{
    NSLog(@"tapAction");
    [self dismiss];
}


- (UIView *)maskView{
    if(!_maskView){
        _maskView = [UIView new];
        _maskView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.8];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [_maskView addGestureRecognizer:tap];
    }
    return _maskView;
}

- (UIView *)containView{
    if(!_containView){
        _containView = [UIView new];
        _containView.layer.cornerRadius = 12.0;
        _containView.backgroundColor = [UIColor whiteColor];
    }
    return _containView;
}

- (UIButton *)canselButton
{
    if(!_canselButton){
        UIButton *_button = [UIButton new];
        [_button setTitle:@"取消" forState:UIControlStateNormal];
        [_button addTarget:self action:@selector(canselAction) forControlEvents:UIControlEventTouchUpInside];
        [_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _canselButton = _button;
    }
    return _canselButton;
}

- (UIButton *)closeButton
{
    if(!_closeButton){
        UIButton *_button = [UIButton new];
        [_button setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [_button addTarget:self action:@selector(canselAction) forControlEvents:UIControlEventTouchUpInside];
        _closeButton = _button;
    }
    return _closeButton;
}



- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.estimatedRowHeight = 50;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.scrollEnabled = NO;
        [_tableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:@"CustomTableViewCell"];
    }
    return _tableView;
}

- (UILabel *)titleLabel
{
    if(!_titleLabel){
        _titleLabel = [UILabel new];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UIView *)spaceView
{
    if(!_spaceView){
        _spaceView = [UIView new];
        _spaceView.backgroundColor = [UIColor lightGrayColor];
    }
    return _spaceView;
}

@end

